package ferp.android.layouts;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews;
import ferp.android.GUI;

/**
 * User: igorgok
 * Date: 1/1/14 3:32 PM
 */
@RemoteViews.RemoteView
public class HorizontalLayout extends LayoutBase
{
  public HorizontalLayout(Context context)
  {
    super(context);
  }

  public HorizontalLayout(Context context, AttributeSet attrs)
  {
    super(context, attrs);
  }

  public HorizontalLayout(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
  }

  protected void onMeasureUnspecified(int widthMeasureSpec, int heightMeasureSpec)
  {
    int count = getChildCount(), visible = 0;
    int mh = 0, width = 0;

    for (int i = 0; i < count; ++i)
    {
      View child = getChildAt(i);

      if (child.getVisibility() != GONE)
      {
        ++visible;

        child.measure(widthMeasureSpec, heightMeasureSpec);

        MarginLayoutParams mlp = (MarginLayoutParams)child.getLayoutParams();
        int cw = child.getMeasuredWidth(), ch = child.getMeasuredHeight();

        //Log.debug(Log.TAG, "HL child #" + visible + " (" + child.getClass().getSimpleName() + ") " + cw + 'x' + ch + ", margins " + mlp.leftMargin + '/' + mlp.topMargin + '/' + mlp.bottomMargin + '/' + mlp.rightMargin);

        mh = Math.max(mh, mlp.topMargin + ch + mlp.bottomMargin);

        width += mlp.leftMargin + cw + mlp.rightMargin;
      }
    }

    //Log.debug(Log.TAG, "HL unspecified total " + width + 'x' + mh);

    setMeasuredDimension(width, mh);
  }

  protected void onMeasureExactly(int sx, int sy)
  {
    int count = getChildCount(), visible = 0;
    int width = 0;

    for (int i = 0; i < count; ++i)
    {
      View child = getChildAt(i);

      if (child.getVisibility() != GONE)
      {
        ++visible;

        MarginLayoutParams mlp = (MarginLayoutParams)child.getLayoutParams();
        // child margins and dimensions are percentage of our dimensions
        int ml = sx * mlp.leftMargin / 100, mr = sx * mlp.rightMargin / 100,
            h;

        switch (mlp.height)
        {
          case LayoutParams.WRAP_CONTENT:
            h = MeasureSpec.UNSPECIFIED;
            break;

          case LayoutParams.MATCH_PARENT:
            h = MeasureSpec.makeMeasureSpec(sy, MeasureSpec.EXACTLY);
            break;

          default:
            h = MeasureSpec.makeMeasureSpec(sy * mlp.height / 100, MeasureSpec.EXACTLY);
            break;
        }

        // set the actual child dimensions
        child.measure(mlp.width == LayoutParams.WRAP_CONTENT ? MeasureSpec.UNSPECIFIED : MeasureSpec.makeMeasureSpec(sx * mlp.width / 100, MeasureSpec.EXACTLY), h);

        //Log.debug(Log.TAG, "HL child #" + visible + " (" + child.getClass().getSimpleName() + ") " + child.getMeasuredWidth() + 'x' + child.getMeasuredHeight() + ", horizontal margins " + ml + '/' + mr);

        width += ml + child.getMeasuredWidth() + mr;
      }
    }

    // starting offset
    offset = (sx - width) / 2;

    //Log.debug(Log.TAG, "HL exact total " + sx + 'x' + sy + ", offset " + offset);

    setMeasuredDimension(sx, sy);
  }

  @Override
  protected void onLayout(boolean b, int left, int top, int right, int bottom)
  {
    int count = getChildCount(), visible = 0;
    int w = right - left, x = offset; // + left;

    //Log.debug(Log.TAG, "HL layout " + left + ',' + top + " - " + right + ',' + bottom + ", offset " + offset + ", count " + count);

    for (int i = 0; i < count; ++i)
    {
      View child = getChildAt(i);

      if (child.getVisibility() != GONE)
      {
        ++visible;

        MarginLayoutParams mlp = (MarginLayoutParams)child.getLayoutParams();
        // calculate child top and bottom margins again
        int ml = w * mlp.leftMargin / 100, mr = w * mlp.rightMargin / 100;
        int cw = child.getMeasuredWidth(), ch = child.getMeasuredHeight(), ct = GUI.center(ch, top, bottom) - top, cl = x + ml, cr = cl + cw;

        child.layout(cl, ct, cr, ct + ch);

        // next horizontal position
        x = cr + mr;

        //Log.debug(Log.TAG, "HL child #" + visible + " (" + child.getClass().getSimpleName() + ") " + child.getLeft() + ',' + child.getTop() + " - " + child.getRight() + ',' + child.getBottom());
      }
    }
  }

  private int offset;
}
